// storage.js
// 本地存储管理接口，使用 chrome.storage.local

const STORAGE_KEY = 'easyotp_accounts';

export function saveAccount(account) {
  getAccountsAsync().then(accounts => {
    accounts.push(account);
    chrome.storage.local.set({ [STORAGE_KEY]: accounts });
  });
}

export function getAccounts() {
  // 同步接口仅供初始渲染用，实际应使用 getAccountsAsync
  // 这里返回空数组，实际渲染时应用 getAccountsAsync
  return [];
}

export function getAccountsAsync() {
  return new Promise(resolve => {
    chrome.storage.local.get([STORAGE_KEY], result => {
      resolve(result[STORAGE_KEY] || []);
    });
  });
}

export function deleteAccount(accountName) {
  getAccountsAsync().then(accounts => {
    const newAccounts = accounts.filter(acc => acc.name !== accountName);
    chrome.storage.local.set({ [STORAGE_KEY]: newAccounts });
  });
} 